/**
 * 项目名称（软件名称）：忠旺机械物资管理系统(mms)
 * 研发设计单位：忠旺机械设备制造有限公司机械设计院智能控制设计科设计与研发。
 * 版权所有：忠旺机械设备制造有限公司
 * 注：本项目的一切技术专利，使用规范归解释权由忠旺机械设备制造有限公司所有。
 * 软件版本：Ver 1.0.0
 * 文件路径：com.zhongwang.mms.module.purchase.controller
 * 创建时间：2020年04月07日 18时17分
 **/
package com.zhongwang.mms.module.purchase.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.module.purchase.model.PAnnexModel;
import com.zhongwang.mms.module.purchase.model.PInventoryMaterielInfoModel;
import com.zhongwang.mms.module.purchase.model.PInventoryModel;
import com.zhongwang.mms.module.purchase.model.PInventorySavePartialModel;
import com.zhongwang.mms.module.purchase.service.PInventoryInfoEditService;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName： PInventoryInfoEditController
 * @Description： 库存信息编辑控制层
 * @Author： 徐一贺
 * @Date： 2020/4/7 18:17
 * @Version： V1.0
 **/
@Log4j2
@Controller
@RequestMapping("/pInventoryInfoEdit")
public class PurPInventoryInfoEditController extends BaseController {

    @Autowired
    private PInventoryInfoEditService pInventoryInfoEditService;

    @GetMapping("/index")
    /**
     *@FunctionName: index
     *@Description: 跳转到库存信息编辑页面
     *@DesignIdea:
     *@Author: 徐一贺
     *@CreateDate: 2020/5/16 15:36
     *@Phone: 18241927380
     *@Param: [model, request] 页面渲染模型和客户请求实体
     *@Return: java.lang.String 页面路径
     *@Throw: 无异常抛出
     *@Version: v 1.0.0
     */
    @RequiresPermissions(value = {"purinv:purinvedit:view"}, logical = Logical.OR)
    public String index(Model model) {
        return "/purchase/purInvManage/pInventoryInfoEdit/page";
    }

    @GetMapping("/gotoLedger/{ityId}")
    /**
     *@FunctionName: index
     *@Description: 跳转到台账页面
     *@DesignIdea: 跳转到台账页面前，获取客户端IP地址，并渲染到页面中，用来记录上传文件者的IP地址
     *@Author: 徐一贺
     *@CreateDate: 2020/5/28 10:09
     *@Phone: 18241927380
     *@Param: [model, request] 页面渲染模型和客户请求实体
     *@Return: java.lang.String 页面路径
     *@Throw: 无异常抛出
     *@Version: v 1.0.0
     */
    public String gotoLedger(@PathVariable("ityId") Integer ityId, Model model, HttpServletRequest request) {
        model.addAttribute("imiItyId", ityId);
        PAnnexModel pAnnexModel = new PAnnexModel();
        pAnnexModel.setAnxItyId(ityId);
        String clientIp = pInventoryInfoEditService.getClientIp(request);
        model.addAttribute("anxUploadIp", clientIp);
        return "/purchase/purInvManage/pInventoryInfoEdit/ledger";
    }

    @GetMapping("/gotoSeeLedger")
    /**
     *@FunctionName: gotoSeeLedger
     *@Description: 跳转到台账查询界面
     *@DesignIdea: 跳转到台账查询界面
     *@Author: 徐一贺
     *@CreateDate: 2020/5/28 16:23
     *@Phone: 18241927380
     *@Return: java.lang.String
     *@Version: v1.0.0
     */
    public String gotoSeeLedger() {
        return "/purchase/purInvManage/pInventoryInfoEdit/ledgerQuery";
    }

    @PostMapping("/inventoryList")
    @ResponseBody
    @RequiresPermissions(value = {"purinv:purinvedit:view"}, logical = Logical.OR)
    /**
     *@FunctionName: findInventoryList
     *@Description: 外购件出库根据条件查询库存信息
     *@DesignIdea: 外购件出库根据条件查询库存信息
     *@Author: 徐一贺
     *@CreateDate: 2020/5/16 15:39
     *@Phone: 18241927380
     *@Param: [param] 查询条件
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Throw: 无异常抛出
     *@Version: v 1.0.0
     */
    public JsonResult findInventoryList(PInventoryModel param) {
        startPage();
        List<PInventoryModel> inventoryList = pInventoryInfoEditService.findInventoryList(param);
        return jsonResult(inventoryList);
    }

    @PostMapping("/getAnxTable")
    @ResponseBody
    @RequiresPermissions(value = {"purinv:purinvedit:edit"}, logical = Logical.OR)
    /**
     *@FunctionName: getAnxTable
     *@Description: 获取附件列表bootstarpTable
     *@DesignIdea: 根据外购件库存主键获取附件列表
     *@Author: 徐一贺
     *@CreateDate: 2020/5/16 15:40
     *@Phone: 18241927380
     *@Param: [param.anxItyId] 外购件库存主键
     *@Return: com.zhongwang.mms.common.JsonResult 列表地数据
     *@Throw: 无异常抛出
     *@Version: v 1.0.0
     */
    public JsonResult getAnxTable(PAnnexModel param) {
        startPage();
        List<PAnnexModel> pAnnexModelList = pInventoryInfoEditService.findPAnnexModelList(param);
        return jsonResult(pAnnexModelList);
    }

    @PostMapping("/getAnnexList")
    @ResponseBody
    /**
     *@FunctionName: getAnnexList
     *@Description: 获取附件列表
     *@DesignIdea: 根据外购件库存主键获取附件列表
     *@Author: 徐一贺
     *@CreateDate: 2020/5/16 15:40
     *@Phone: 18241927380
     *@Param: [param.anxItyId] 外购件库存主键
     *@Return: com.zhongwang.mms.common.JsonResult 列表地数据
     *@Throw: 无异常抛出
     *@Version: v 1.0.0
     */
    public JsonResult getAnnexList(PAnnexModel param) {
        List<PAnnexModel> pAnnexModelList = pInventoryInfoEditService.findPAnnexModelList(param);
        return JsonResult.success("", pAnnexModelList);
    }

    @PostMapping("/save")
    @ResponseBody
    @RequiresPermissions(value = {"purinv:purinvedit:edit"}, logical = Logical.OR)
    /**
     *@FunctionName: save
     *@Description: 保存外购件库存要修改的信息
     *@DesignIdea: 保存页面传递来要修改的外购件库存信息
     *@Author: 徐一贺
     *@CreateDate: 2020/5/16 15:41
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult 保存结果
     *@Throw: 无异常抛出
     *@Version: v 1.0.0
     */
    public JsonResult save(PInventorySavePartialModel param) {
        boolean result = pInventoryInfoEditService.save(param);
        if (!result) {
            return JsonResult.fail("修改失败！");
        }
        return JsonResult.success("修改成功！");
    }

    @PostMapping("/upload")
    @ResponseBody
    @RequiresPermissions(value = {"purinv:purinvedit:edit"}, logical = Logical.OR)
    /**
     *@FunctionName: upload
     *@Description: 文件上传
     *@DesignIdea: 上传文件，并根据客户端传递来的外购件库存主键和客户端IP生成附件列表。
     *@Author: 徐一贺
     *@CreateDate: 2020/5/16 15:42
     *@Phone: 18241927380
     *@Param: [file, anxItyId, anxUploadIp]上传的文件、外购件库存主键，客户端IP地址
     *@Return: com.zhongwang.mms.common.JsonResult 上传结果
     *@Throw: 无异常抛出
     *@Version: v 1.0.0
     */
    public JsonResult upload(@RequestParam("file") MultipartFile file,
                             @RequestParam("anxItyId") String anxItyId,
                             @RequestParam("anxUploadIp") String anxUploadIp) {
        String result = pInventoryInfoEditService.upload(file, anxItyId, anxUploadIp);
        if (result.equals("上传失败！")) {
            return JsonResult.fail(result);
        } else if (result.equals("上传成功！")) {
            return JsonResult.success(result);
        }
        return JsonResult.fail(result);
    }

    @GetMapping("/download/{anxId}")
    @RequiresPermissions(value = {"purinv:purledgerview:download", "purinv:purinvedit:download"}, logical = Logical.OR)
    /**
     *@FunctionName: download
     *@Description: 文件下载
     *@DesignIdea: 根据外购件附件表主键获取文件信息，进行下载。
     *@Author: 徐一贺
     *@CreateDate: 2020/5/16 15:44
     *@Phone: 18241927380
     *@Param: [anxId, response] 外购件附件表主键、向客户端返回的数据
     *@Return: void
     *@Throw: 无异常抛出
     *@Version: v 1.0.0
     */
    public void download(@PathVariable("anxId") String anxId, HttpServletResponse response) {
        pInventoryInfoEditService.download(anxId, response);
    }

    @PostMapping("/removeFile")
    @ResponseBody
    @RequiresPermissions(value = {"purinv:purledgeredit:del"}, logical = Logical.OR)
    /**
     *@FunctionName: removeFile
     *@Description: 删除文件
     *@DesignIdea: 根据外购件附件表主键删除文件以及附件信息
     *@Author: 徐一贺
     *@CreateDate: 2020/5/16 15:44
     *@Phone: 18241927380
     *@Param: [param.anxId] 外购件附件表主键
     *@Return: void
     *@Throw: 无异常抛出
     *@Version: v 1.0.0
     */
    public JsonResult removeFile(PAnnexModel param) {
        return pInventoryInfoEditService.removeFile(param);
    }

    @PostMapping("/findPInventoryMaterielInfoList/{imiItyId}")
    @ResponseBody
    /**
     *@FunctionName: findPInventoryMaterielInfoList
     *@Description: 加载台账编辑列表
     *@DesignIdea: 加载台账编辑列表
     *@Author: 徐一贺
     *@CreateDate: 2020/5/27 15:09
     *@Phone: 18241927380
     *@Param: [param] 查询参数
     *@Return: com.zhongwang.mms.common.JsonResult 结果
     *@Throw: 无异常抛出
     *@Version: 1.0.0
     */
    public JsonResult findPInventoryMaterielInfoList(@PathVariable("imiItyId") Integer imiItyId) {
        startPage();
        PInventoryMaterielInfoModel param = new PInventoryMaterielInfoModel();
        param.setImiItyId(imiItyId);
        return jsonResult(pInventoryInfoEditService.findPInventoryMaterielInfoList(param));
    }

    @PostMapping("/findPInventoryMaterielInfoList")
    @ResponseBody
    @RequiresPermissions(value = {"purinv:purinvedit:view", "mecinv:purledgerview:view"}, logical = Logical.OR)
    /**
     *@FunctionName: findPInventoryMaterielInfoList
     *@Description: 加载台账查询列表
     *@DesignIdea: 加载台账查询列表
     *@Author: 徐一贺
     *@CreateDate: 2020/5/27 15:09
     *@Phone: 18241927380
     *@Param: [param] 查询参数
     *@Return: com.zhongwang.mms.common.JsonResult 结果
     *@Throw: 无异常抛出
     *@Version: 1.0.0
     */
    public JsonResult findPInventoryMaterielInfoList(PInventoryMaterielInfoModel param) {
        startPage();
        return jsonResult(pInventoryInfoEditService.findPInventoryMaterielInfoList(param));
    }

    @PostMapping("/saveLedger")
    @ResponseBody
    @RequiresPermissions(value = {"purinv:purledgeredit:edit"}, logical = Logical.OR)
    /**
     *@FunctionName: addLedger
     *@Description: 保存台账
     *@Author: 徐一贺
     *@CreateDate: 2020/5/27 16:42
     *@Phone: 18241927380
     *@Param: [param] 台账信息
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: v1.0.0
     */
    public JsonResult saveLedger(PInventoryMaterielInfoModel param) {
        return pInventoryInfoEditService.saveLedger(param);
    }

    @PostMapping("/removeLedger")
    @ResponseBody
    @RequiresPermissions(value = {"purinv:purledgeredit:del"}, logical = Logical.OR)
    /**
     *@FunctionName: removeLedger
     *@Description: 删除台账信息
     *@DesignIdea: 根据主键删除台账信息
     *@Author: 徐一贺
     *@CreateDate: 2020/5/28 8:28
     *@Phone: 18241927380
     *@Param: [param.imiId] 台账主键
     *@Return: com.zhongwang.mms.common.JsonResult 结果
     *@Version: v1.0.0
     */
    public JsonResult removeLedger(PInventoryMaterielInfoModel param) {
        return pInventoryInfoEditService.removeLedger(param);
    }

    @PostMapping("/getLedger")
    @ResponseBody
    @RequiresPermissions(value = {"purinv:purinvedit:edit"}, logical = Logical.OR)
    /**
     *@FunctionName: getLedger
     *@Description: 获取台账信息
     *@DesignIdea: 根据主键获取台账信息
     *@Author: 徐一贺
     *@CreateDate: 2020/5/28 8:28
     *@Phone: 18241927380
     *@Param: [param.imiId] 台账主键
     *@Return: com.zhongwang.mms.common.JsonResult 结果
     *@Version: v1.0.0
     */
    public JsonResult getLedger(PInventoryMaterielInfoModel param) {
        return pInventoryInfoEditService.getLedger(param);
    }

    @PostMapping("/getPInventoryList")
    @ResponseBody
    /**
     *@FunctionName: getPInventoryList
     *@Description: 获取库存信息列表
     *@DesignIdea: 获取库存信息列表
     *@Author: 徐一贺
     *@CreateDate: 2020年7月11日10:12:28
     *@Phone: 18241927380
     *@Param: [param.imiId] 台账主键
     *@Return: com.zhongwang.mms.common.JsonResult 结果
     *@Version: v1.0.0
     */
    public JsonResult getPInventoryList(PInventoryModel param) {
        return JsonResult.success("", pInventoryInfoEditService.getPInventoryList(param));
    }
}